package cn.iocoder.yudao.module.ordering.controller.admin.food;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.ordering.controller.admin.food.vo.FoodPageReqVO;
import cn.iocoder.yudao.module.ordering.controller.admin.food.vo.FoodRespVO;
import cn.iocoder.yudao.module.ordering.controller.admin.food.vo.FoodSaveReqVO;
import cn.iocoder.yudao.module.ordering.controller.admin.food.vo.MyOrderingReqVO;
import cn.iocoder.yudao.module.ordering.controller.admin.food.vo.MyOrderingRespVO;
import cn.iocoder.yudao.module.ordering.dal.dataobject.food.FoodDO;
import cn.iocoder.yudao.module.ordering.service.food.FoodService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 订餐-餐品信息")
@RestController
@RequestMapping("/ordering/food")
@Validated
public class FoodController {

    @Resource
    private FoodService foodService;

    @PostMapping("/create")
    @Operation(summary = "创建订餐-餐品信息")
    @PreAuthorize("@ss.hasPermission('ordering:food:create')")
    public CommonResult<Long> createFood(@Valid @RequestBody FoodSaveReqVO createReqVO) {
        return success(foodService.createFood(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新订餐-餐品信息")
    @PreAuthorize("@ss.hasPermission('ordering:food:update')")
    public CommonResult<Boolean> updateFood(@Valid @RequestBody FoodSaveReqVO updateReqVO) {
        foodService.updateFood(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除订餐-餐品信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ordering:food:delete')")
    public CommonResult<Boolean> deleteFood(@RequestParam("id") Long id) {
        foodService.deleteFood(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得订餐-餐品信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ordering:food:query')")
    public CommonResult<FoodRespVO> getFood(@RequestParam("id") Long id) {
        FoodDO food = foodService.getFood(id);
        return success(BeanUtils.toBean(food, FoodRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得订餐-餐品信息分页")
    @PreAuthorize("@ss.hasPermission('ordering:food:query')")
    public CommonResult<PageResult<FoodRespVO>> getFoodPage(@Valid FoodPageReqVO pageReqVO) {
        PageResult<FoodDO> pageResult = foodService.getFoodPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, FoodRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出订餐-餐品信息 Excel")
    @PreAuthorize("@ss.hasPermission('ordering:food:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportFoodExcel(@Valid FoodPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<FoodDO> list = foodService.getFoodPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "订餐-餐品信息.xls", "数据", FoodRespVO.class,
                        BeanUtils.toBean(list, FoodRespVO.class));
    }

    @GetMapping("/getMyOrderingList")
    @Operation(summary = "查询员工订餐列表")
    @PreAuthorize("@ss.hasPermission('ordering:myordering:query')")
    public CommonResult<List<MyOrderingRespVO>> getMyOrderingList(@Valid MyOrderingReqVO reqVO) {
        List<MyOrderingRespVO> listResult = foodService.getMyOrderingList(reqVO);
        return success(listResult);
    }
}
